package com.github.soiff.shadow.plugin;

import com.github.soiff.shadow.api.Plugin;
import com.github.soiff.shadow.definition.Global;
import com.github.soiff.shadow.definition.Handler;
import com.github.soiff.shadow.plugin.common.Component;
import com.github.soiff.shadow.plugin.common.I18nResource;
import com.github.soiff.shadow.plugin.helper.InjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.Environment;

import java.util.*;

/**
 * Created by Soiff on 2016/05/19.
 *
 * @author : Soiff
 */
public class SpringPlugin implements Plugin {

    public static Global context;

    public final static Logger log = LoggerFactory.getLogger(SpringPlugin.class);

    public static Component findComponent (String key, String path) {
        // TODO: return proper component information.
        return new Component();
    }

    public static String execComponent (Component component, Map map) {
        // TODO: return handled result.
        return "TODO: return handled result.";
    }

    public static I18nResource getResource(Object app, String baseName, Locale locale) {
        // TODO: add proper i18n feature.
        return new I18nResource(new HashMap<>(), Locale.CHINA);
    }

    @Override
    public boolean initial(Global context) {
        SpringPlugin.context = context;
        log.info(SpringPlugin.class.getName() + " has been initialized successfully.");
        return true;
    }

    @Override
    public Environment environment() {
        return null;
    }

    @Override
    public String resources() {
        return "WEB-INF/web";
    }

    @Override
    public List<Handler> handlers() {
        return Arrays.asList(new Handler(InjectHelper.directive, new InjectHelper()));
    }

    @Override
    public boolean update() {
        return false;
    }

    @Override
    public boolean remove() {
        return false;
    }

    @Override
    public boolean reload() {
        return false;
    }

    public static boolean isClearInjectNestedContext() {
        final AbstractEnvironment environment = context.getEnvironment();
        return null != environment ? environment.getProperty("isClearInjectNestedContext", Boolean.class, false) : false;
    }
    public static boolean isDevMode() {
        final AbstractEnvironment environment = context.getEnvironment();
        return null != environment ? environment.getProperty("isDevMode", Boolean.class, false) : false;
    }
}
